package com.example.enjoyarkuix;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.enjoyarkuix.stagefragment.NativeFragment;
import com.example.enjoyarkuix.stagefragment.OneFragment;
import com.example.enjoyarkuix.stagefragment.TwoFragment;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerFragmentActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i("HiHelloWorld", "SingleModuleActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_viewpager_fragment);

        ImageView ivTopBarBack= (ImageView) findViewById(R.id.ivTopBarBack);
        ivTopBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView title  = (TextView) findViewById(R.id.tvTopBarTitle);
        title.setText("原生Activity");

        OneFragment fragment = new OneFragment();
        Bundle args = new Bundle();
        args.putString("params", "{\"params\":[{\"key\":\"content\",\"type\":10,\"value\":\"hello,stageFragment\"}，{\"key\":\"from\",\"type\":10,\"value\":\"消息来自activity\"}]}");
        fragment.setArguments(args);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(fragment);
        fragments.add(new TwoFragment());
        fragments.add(new NativeFragment());


        TabLayout tabLayout=findViewById(R.id.tabLayout);
        ViewPager viewPager=findViewById(R.id.viewPager);

        TitleFragmentPagerAdapter adapter = new TitleFragmentPagerAdapter(getSupportFragmentManager(), fragments, new String[]{"StageFragment", "StageFragment","原生Fragment"});
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }
    public class TitleFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragmentList = null;

        private String[] titles;

        public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
                                         ArrayList<Fragment> fragmentList) {
            super(mFragmentManager);
            mFragmentList = fragmentList;
        }

        public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
                                         List<Fragment> fragmentList, String[] titles) {
            super(mFragmentManager);
            mFragmentList = fragmentList;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = null;
            if (position < mFragmentList.size()) {
                fragment = mFragmentList.get(position);
            } else {
                fragment = mFragmentList.get(0);
            }
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (titles != null && titles.length > 0)
                return titles[position];
            return null;
        }
    }
}
